import { accountPermType, appTreeType, ResultCommon, ResultCommonList, serviceType } from "@/utils/commonType";
import { http } from "@/utils/http";
import { baseUrlApi } from "@/utils/http/utils";
import { removeEmptyValues } from "@/utils/common";

export const getAppTreeList = async (tenantId = "") => {
  let result = null;
  if (tenantId == "") {
    result = await http.request<ResultCommonList<appTreeType>>(
      "get",
      baseUrlApi("/marks/appList")
    );
  } else {
    result = await http.request<ResultCommonList<appTreeType>>(
      "get",
      baseUrlApi("/marks/appList"),
      { params: { tenantId: tenantId } }
    );
  }

  if (result.code != 200) {
    throw new Error(result.message);
  }
  return result;
};

export const getServiceList = async (account: string, gId?: string, type?: string) => {
  const params = { account: account, gId: gId, type: type };
  const result = await http.request<ResultCommonList<serviceType>>(
    "get",
    baseUrlApi("/marks/serviceList"), { params: removeEmptyValues(params) }
  );
  if (result.code != 200) {
    throw new Error(result.message);
  }
  return result;
};

export const getAccountServiceList = async (account: string, gId?: string, type?: string) => {
  const params = { account: account, gId: gId, type: type };
  const result = await http.request<ResultCommonList<serviceType>>(
    "get",
    baseUrlApi("/marks/accountServiceList"), { params: removeEmptyValues(params) }
  );
  if (result.code != 200) {
    throw new Error(result.message);
  }
  return result;
};

export const subjectApi = async (accountPerm: accountPermType) => {
  const result = await http.request<ResultCommon<boolean>>(
    "post",
    baseUrlApi("/marks/subService"), { data: removeEmptyValues(accountPerm) }
  );
  if (result.code != 200) {
    throw new Error(result.message);
  }
  return result;
}

export const unSubjectApi = async (serviceId: string, account: string) => {
  const params = { serviceId: serviceId, account: account };
  const result = await http.request<ResultCommon<boolean>>(
    "delete",
    baseUrlApi("/marks/unSubService"), { params: removeEmptyValues(params) }
  );
  if (result.code != 200) {
    throw new Error(result.message);
  }
  return result;
}